package com.ctsi.ssdc.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ctsi.ssdc.admin.domain.CscpUser;
import com.ctsi.ssdc.admin.domain.CscpUserRole;
import com.ctsi.ssdc.admin.domain.dto.CscpUserDetailDTO;
import com.ctsi.ssdc.admin.domain.dto.CscpUserRoleDTO;
import com.ctsi.ssdc.admin.repository.CscpUserDetailRepository;
import com.ctsi.ssdc.admin.repository.CscpUserRepository;
import com.ctsi.ssdc.admin.service.CscpUserDetailService;
import com.ctsi.ssdc.admin.service.CscpUserRoleService;
import com.ctsi.ssdc.admin.service.CscpUserService;
import com.ctsi.ssdc.admin.service.mapper.CscpUserDetailMapper;
import com.ctsi.ssdc.service.SSOService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Api(value = "/api", tags = {"b-project-base-info-controller"})
@RestController
@RequestMapping("/sso")
@Slf4j
public class SSOController {

    @Resource
    private SSOService ssoService;

    @PostMapping("getThirdPartyUserInfo")
    public Boolean getThirdPartyUserInfo(@RequestParam("token") String token) {
        log.info("-------------------------------------------\n\t" +
                        "登录时间: {}\n\t" +
                        "登录token: {}\n" +
                        "-------------------------------------------\n",
                new Date(),
                token);
        // 1. 发出请求
        CscpUserDetailDTO userDetailDTO = ssoService.fetchSSOUserDetail(token);
        if (ObjectUtil.isNull(userDetailDTO)) throw new RuntimeException("请求失败");
        //1、解析json，如果数据库不存在该用户信息，将用户信息存入数据库
        Boolean exist = ssoService.userExist(userDetailDTO);
        // 判断存在与否
        if (!exist) {
            // 添加数据库
            CscpUserDetailDTO cscpUserDetailDTO = ssoService.saveUser(userDetailDTO);
        }
        return true;
    }
}
